<!--分类管理页-->
<template>
  <a-layout>
    <a-layout-content :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }">
      <a-form layout="inline" :model="param">
        <a-form-item>
          <a-button type="primary" @click="handleQuery()">
            查询
          </a-button>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="add()">
            新增
          </a-button>
        </a-form-item>
      </a-form>
      <a-table
          :columns="columns"
          :row-key="record => record.id"
          :data-source="categorys"
          :pagination="false"
          :loading="loading"
      >
        <template #cover="{ text: cover }">
          <img v-if="cover" :src="cover" alt="avatar" />
        </template>
        <template v-slot:action="{ text, record }">
          <a-space size="small">
              <a-button type="primary" @click="edit(record)">
                编辑
              </a-button>
              <a-popconfirm
                title="删除后不可恢复，确认删除？"
                ok-text="是"
                cancel-text="否"
                @confirm="handleDelete(record.id)"
              >
                <a-button danger>
                  删除
                </a-button>
             </a-popconfirm>
          </a-space>
        </template>
      </a-table>
    </a-layout-content>
  </a-layout>
  <!-- 编辑 -->
  <a-modal v-model:visible="modalVisible" title="分类表单" :confirm-loading="confirmLoading" @ok="handleOk">
    <a-form
        :model="category"
        :label-col="{ span: 8 }"
        :wrapper-col="{ span: 16 }"
        autocomplete="off"
    >
      <a-form-item label="名称">
        <a-input v-model:value="category.name" />
      </a-form-item>
      <a-form-item label="父分类">
        <a-select v-model:value="category.parent">
          <a-select-option value="0">
            无
          </a-select-option>
          <a-select-option v-for="c in categorys" :key="c.id" :value="c.id" :disabled="category.id === c.id">
            {{c.name}}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="顺序">
        <a-input v-model:value="category.sort" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script lang="ts">
import { SmileOutlined, DownOutlined } from '@ant-design/icons-vue';
import {defineComponent, onMounted, ref} from 'vue';
import axios from "axios";
import { message } from "ant-design-vue";
import {Tool} from "@/util/tool";

export default defineComponent({
  name: 'admin-category',
  components: {
    SmileOutlined,
    DownOutlined,
  },
  setup() {
    const categorys = ref();
    // 分类查询参数封装
    const param = ref();
    param.value ={};
    const loading = ref(false);

    const columns = [
      {
        title: '名称',
        dataIndex: 'name'
      },
      {
        title: '父分类',
        key: 'parent',
        dataIndex: 'parent'
      },
      {
        title: '顺序',
        dataIndex: 'sort'
      },
      {
        title: 'Action',
        key: 'action',
        slots: { customRender: 'action' }
      }
    ];

    /**
     * 数据查询
     **/
    const handleQuery = () => {
      loading.value = true;
      // 这里get传参不用拼接方法 使用params固定参数配置
      axios.get("/category/all").then((response) => {
        loading.value = false;
        const data = response.data;
        if (data.success) {
          //categorys.value = data.content; // 这是数组结构
          categorys.value = Tool.array2Tree(data.content, 0); // 自己处理成树形结构
          console.log("树形结构：", categorys.value);
        } else {
          message.error(data.message);
        }
      });
    }

    // 弹出编辑框
    const modalVisible = ref(false);
    const confirmLoading = ref<boolean>(false);
    // --------表单数据-----------
    const category = ref();
    const edit = (record: any) => {
      modalVisible.value = true;
      // category.value = record; 此写法会是响应式的 编辑应该是编辑赋值的对象，通过JSON方法实现  这里封装成工具类
      // category.value = JSON.parse(JSON.stringify(record));
      category.value = Tool.copy(record);
    };
    // 编辑框确认
    const handleOk = () => {
      confirmLoading.value = true;
      // 不像get,传params键
      axios.post("/category/save", category.value).then((resp) => {
        const data = resp.data;
        confirmLoading.value = false;
        // 成功
        if (data.success) {
          modalVisible.value = false;
          // 刷新列表
          handleQuery();
        } else {
          message.error(data.message);
        }
      })
    };

    // 新增分类
    const add = () => {
      modalVisible.value = true;
      category.value = {};
    };
    const handleDelete = (id:number)=> {
      axios.delete("/category/delete/" + id).then((resp) => {
        const data = resp.data;
        // 成功
        if (data.success) {
          // 刷新列表
          handleQuery();
        }
      })
    }

    onMounted(() => {
      handleQuery();
    });

    return {
      categorys,
      param,
      loading,
      columns,
      category,
      modalVisible,
      confirmLoading,
      edit,
      add,
      handleOk,
      handleDelete,
      handleQuery,
    };
  },
});
</script>


<style scoped>
img {
  width: 50px;
  height: 50px;
}
</style>